package com.zwh.common.easyexcel.converters;

import java.math.BigDecimal;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @author monkey
 * @date 2021/1/28 15:30
 **/
public class BigDecimalStringConvert implements Converter<String> {
    /**
     * 支持转换的java类
     * @return
     */
    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    /**
     * 支持的excel类型，读取时候使用
     * @return
     */
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return cellData.getNumberValue().toPlainString();
    }

    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData(new BigDecimal(value));
    }
}
